package nsu.edu.zsq.service.impl.sys;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.base.Splitter;
import nsu.edu.zsq.bean.SysAcl;
import nsu.edu.zsq.bean.SysAclExample;
import nsu.edu.zsq.bean.SysUser;
import nsu.edu.zsq.bean.SysUserExample;
import nsu.edu.zsq.common.RequestHolder;
import nsu.edu.zsq.dao.SysAclMapper;
import nsu.edu.zsq.dao.SysUserMapper;
import nsu.edu.zsq.service.sys.SysUserManagerService;
import nsu.edu.zsq.util.BeanValidator;
import nsu.edu.zsq.util.PasswordUtil;

/** 
* Description: 管理员账号管理的service实现类<br>
*/
@Service
public class SysUserManagerServiceImpl implements SysUserManagerService {
    
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysAclMapper sysAclMapperl;

    @Override
    public SysUser getSysUser(int sysUserId) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(sysUserId);
        return sysUser;
    }

    @Override
    public List<SysUser> listSysUser() {
        SysUserExample example = new SysUserExample();
        // 1：普通管理员 0：超级管理员
        example.createCriteria().andTypeEqualTo(1);
        List<SysUser> listSysUser = sysUserMapper.selectByExample(example);
        return listSysUser;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveSysUser(SysUser sysUser) {
        BeanValidator.check(sysUser);
        // 默认都是普通管理员
        sysUser.setType(1);
        // 默认不能访问“管理员账号管理”内容
        sysUser.setBanUrl("/sys/sysUser");
        // 密码加密
        sysUser.setPassword(PasswordUtil.md5(sysUser.getPassword()));
        sysUser.setOperator(RequestHolder.getCurrentSysUser().getAccount());
        sysUser.setOperatorTime(new Date());
        sysUserMapper.insertSelective(sysUser);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSysUser(SysUser sysUser) {
        sysUser.setOperator(RequestHolder.getCurrentSysUser().getAccount());
        sysUser.setOperatorTime(new Date());
        sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delSysUser(int[] sysUserIds) {
        for (int sysUserId : sysUserIds) {
            sysUserMapper.deleteByPrimaryKey(sysUserId);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatePassword(SysUser sysUser) {
        // 密码加密
        sysUser.setPassword(PasswordUtil.md5(sysUser.getPassword()));
        sysUser.setOperator(RequestHolder.getCurrentSysUser().getAccount());
        sysUser.setOperatorTime(new Date());
        sysUserMapper.updateByPrimaryKeySelective(sysUser);
        
    }

    @Override
    public List<SysAcl> sysUserAclTree(int sysUserId) {
        SysUser sysUser = getSysUser(sysUserId);
        SysAclExample example = new SysAclExample();
        List<SysAcl> listSysAcl = sysAclMapperl.selectByExample(example);
        for (SysAcl sysAcl : listSysAcl) {
            List<String> banUrlList = Splitter.on(";").trimResults().omitEmptyStrings().splitToList(sysUser.getBanUrl());
            // 勾选该管理员已经存在的受限url
            if (banUrlList !=null && banUrlList.contains(sysAcl.getBanUrl())) {
                sysAcl.setChecked((byte)1);
            }
        }
        return listSysAcl;
    }

    @Override
    public void updateBanUrl(String[] addBanUrl, String[] delBanUrl, int sysUserId) {
        SysUser sysUser = getSysUser(sysUserId);
        List<String> oldBanUrlList = Splitter.on(";").trimResults().omitEmptyStrings().splitToList(sysUser.getBanUrl());
        StringBuilder newBanUrl = new StringBuilder();
        // 1.删除受限url
        if (delBanUrl !=null && delBanUrl.length > 0) {
            List<String> delBanUrlList = Arrays.asList(delBanUrl);
            for (String banUrl : oldBanUrlList) {
                if (!delBanUrlList.contains(banUrl)) {
                    newBanUrl.append(banUrl);
                    newBanUrl.append(";");
                }
            }
        } else {
            for (String banUrl : oldBanUrlList) {
                newBanUrl.append(banUrl);
                newBanUrl.append(";");
            }
        }
        // 2.添加受限url
        if (addBanUrl !=null && addBanUrl.length > 0) {
            for (String banUrl : addBanUrl) {
                newBanUrl.append(banUrl);
                newBanUrl.append(";");
            }
        }
        newBanUrl.deleteCharAt(newBanUrl.length() - 1);
        // 3.更新url
        SysUser newSysUser = SysUser.builder().id(sysUser.getId()).banUrl(newBanUrl.toString()).build();
        sysUserMapper.updateByPrimaryKeySelective(newSysUser);        
    }

}
